package com.jxw.update;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.Fragment;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.text.ttml.TtmlNode;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.gson.Gson;
import com.jxw.launcher.R;
import com.jxw.launcher.model.UpdateBean;
import com.jxw.launcher.util.HttpDownListener;
import com.jxw.launcher.util.OkHttpDownUtil;
import com.jxw.launcher.util.ReceiverUtil;
import com.jxw.launcher.util.SystemPropertiesProxy;
import com.jxw.launcher.util.Util;
import com.wang.avi.AVLoadingIndicatorView;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import net.lingala.zip4j.util.InternalZipConstants;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.json.JSONException;
import org.json.JSONObject;

/* loaded from: classes.dex */
public class Fragment2 extends Fragment {
    private static String VERSION_URL = "https://www.xiaomi.smart0.cn/mono-biz-app/update/getVersionFlat";
    private static String appKey = "jxw0190702";
    private static String appSecret = "1445d918439080212345678456132342341bd3a2d212121";
    private String DOWN_PATH;
    private String apkName;
    private AVLoadingIndicatorView avi;
    private LinearLayout ll_new_version;
    private LinearLayout ll_no_net;
    private LinearLayout ll_no_new_version;
    private LinearLayout ll_to_down;
    private TextView tv_newVersion_down;
    private TextView tv_newVersion_size_down;
    private TextView tv_no_version;
    private TextView tv_set_net;
    private TextView tv_to_down;
    private TextView tv_update_content;
    private TextView tv_version;
    private String versionCode;
    private String SYS_DOWN_URL = "";
    private BroadcastReceiver networkReceiver = new BroadcastReceiver() { // from class: com.jxw.update.Fragment2.4
        @Override // android.content.BroadcastReceiver
        public void onReceive(Context context, Intent intent) {
            if (ReceiverUtil.ACTION_CONNECTIVITY_ACTION.equals(intent.getAction())) {
                NetworkInfo activeNetworkInfo = ((ConnectivityManager) context.getSystemService("connectivity")).getActiveNetworkInfo();
                if (activeNetworkInfo == null) {
                    Fragment2.this.ll_no_net.setVisibility(0);
                    Fragment2.this.ll_new_version.setVisibility(8);
                } else if (activeNetworkInfo.isConnected()) {
                    Fragment2.this.ll_no_net.setVisibility(8);
                    Fragment2.this.ll_new_version.setVisibility(0);
                    Fragment2.this.getVersionCode();
                } else {
                    Fragment2.this.ll_no_net.setVisibility(0);
                    Fragment2.this.ll_new_version.setVisibility(8);
                }
            }
        }
    };

    @Override // android.support.v4.app.Fragment
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        View inflate = layoutInflater.inflate(R.layout.fragment_2_2, viewGroup, false);
        initView(inflate);
        return inflate;
    }

    private void initView(View view) {
        this.ll_no_net = (LinearLayout) view.findViewById(R.id.ll_no_net);
        this.ll_new_version = (LinearLayout) view.findViewById(R.id.ll_new_version);
        this.ll_no_new_version = (LinearLayout) view.findViewById(R.id.ll_no_new_version);
        this.ll_to_down = (LinearLayout) view.findViewById(R.id.ll_to_down);
        this.tv_set_net = (TextView) view.findViewById(R.id.tv_set_net);
        this.tv_version = (TextView) view.findViewById(R.id.tv_version);
        this.tv_no_version = (TextView) view.findViewById(R.id.tv_no_version);
        this.tv_update_content = (TextView) view.findViewById(R.id.tv_update_content);
        this.tv_newVersion_down = (TextView) view.findViewById(R.id.tv_newVersion_down);
        this.tv_newVersion_size_down = (TextView) view.findViewById(R.id.tv_newVersion_size_down);
        this.tv_to_down = (TextView) view.findViewById(R.id.tv_to_down);
        this.avi = (AVLoadingIndicatorView) view.findViewById(R.id.avi);
        this.avi.show();
        getActivity().registerReceiver(this.networkReceiver, new IntentFilter(ReceiverUtil.ACTION_CONNECTIVITY_ACTION));
        this.DOWN_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/appResource";
        File file = new File(this.DOWN_PATH);
        if (!file.exists()) {
            try {
                file.mkdirs();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        this.tv_to_down.setOnClickListener(new View.OnClickListener() { // from class: com.jxw.update.Fragment2.1
            @Override // android.view.View.OnClickListener
            public void onClick(View view2) {
                if (!TextUtils.isEmpty(Fragment2.this.SYS_DOWN_URL)) {
                    Fragment2.this.tv_to_down.setEnabled(false);
                    Fragment2.this.getToDownLinner();
                    return;
                }
                Toast.makeText(Fragment2.this.getContext(), "下载地址为空", 0).show();
            }
        });
        this.tv_set_net.setOnClickListener(new View.OnClickListener() { // from class: com.jxw.update.Fragment2.2
            @Override // android.view.View.OnClickListener
            public void onClick(View view2) {
                Fragment2.this.startActivity(new Intent("android.settings.WIFI_SETTINGS"));
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void getToDownLinner() {
        OkHttpDownUtil okHttpDownUtil = OkHttpDownUtil.getInstance();
        String str = this.SYS_DOWN_URL;
        okHttpDownUtil.getRenewalDownRequest(str, new File(this.DOWN_PATH + this.apkName), new HttpDownListener() { // from class: com.jxw.update.Fragment2.3
            @Override // com.jxw.launcher.util.HttpDownListener
            public void onFailure(Call call, IOException iOException) {
                Log.i("==error==", iOException.getMessage() + "");
            }

            @Override // com.jxw.launcher.util.HttpDownListener
            public void onResponse(Call call, Response response, final long j, final long j2) {
                Fragment2.this.getActivity().runOnUiThread(new Runnable() { // from class: com.jxw.update.Fragment2.3.1
                    @Override // java.lang.Runnable
                    public void run() {
                        if (j == j2) {
                            Fragment2.this.tv_to_down.setText("下载完成");
                            OkHttpDownUtil.getInstance().destroy();
                            Fragment2.this.install();
                            Fragment2.this.tv_to_down.setEnabled(true);
                            return;
                        }
                        TextView textView = Fragment2.this.tv_to_down;
                        StringBuilder sb = new StringBuilder();
                        sb.append("下载中...");
                        double d = j2;
                        Double.isNaN(d);
                        double d2 = j;
                        Double.isNaN(d2);
                        sb.append((int) (((d * 1.0d) / d2) * 100.0d));
                        sb.append("%");
                        textView.setText(sb.toString());
                    }
                });
            }
        });
    }

    @Override // android.support.v4.app.Fragment
    public void onHiddenChanged(boolean z) {
        super.onHiddenChanged(z);
        if (z) {
            AVLoadingIndicatorView aVLoadingIndicatorView = this.avi;
            if (aVLoadingIndicatorView != null) {
                aVLoadingIndicatorView.setVisibility(4);
                this.avi.hide();
                return;
            }
            return;
        }
        AVLoadingIndicatorView aVLoadingIndicatorView2 = this.avi;
        if (aVLoadingIndicatorView2 != null) {
            aVLoadingIndicatorView2.setVisibility(0);
            this.avi.show();
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void getVersionCode() {
        String str;
        String str2 = VERSION_URL;
        Log.i("TAG", "请求链接：" + str2);
        String serialNumber = Util.getSerialNumber(getContext());
        try {
            str = getActivity().getPackageManager().getPackageInfo(getActivity().getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            str = null;
        }
        final String string = SystemPropertiesProxy.getString(getActivity(), "ro.build.display.id");
        String str3 = Build.VERSION.RELEASE;
        TextView textView = this.tv_version;
        textView.setText("当前版本：" + string);
        long currentTimeMillis = System.currentTimeMillis();
        String stringToMD5 = Util.stringToMD5(appKey + currentTimeMillis + appSecret);
        HashMap hashMap = new HashMap();
        if (!TextUtils.isEmpty(serialNumber)) {
            hashMap.put("uniqueNumber", serialNumber);
        } else {
            hashMap.put("uniqueNumber", "");
        }
        hashMap.put("versionName", str);
        hashMap.put("firmwareVersion", string);
        hashMap.put("JXWAppkey", appKey);
        hashMap.put("JXWSalt", Long.valueOf(currentTimeMillis));
        hashMap.put("JXWSign", stringToMD5);
        HashMap hashMap2 = new HashMap();
        hashMap2.put("ver", str);
        hashMap2.put(NotificationCompat.CATEGORY_SYSTEM, "andriod");
        hashMap2.put("sysVer", str3);
        hashMap2.put(TtmlNode.TAG_BODY, hashMap);
        String json = new Gson().toJson(hashMap2);
        Log.i("==param===", json);
        new OkHttpClient().newCall(new Request.Builder().url(str2).post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json)).addHeader("Content-Type", "application/json").build()).enqueue(new Callback() { // from class: com.jxw.update.Fragment2.5
            @Override // okhttp3.Callback
            public void onFailure(Call call, IOException iOException) {
            }

            @Override // okhttp3.Callback
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    try {
                        JSONObject jSONObject = new JSONObject(response.body().string());
                        Log.i("TAG", "请求结果：" + jSONObject.toString());
                        if (jSONObject.getInt("code") == 8000) {
                            final UpdateBean updateBean = (UpdateBean) com.alibaba.fastjson.JSONObject.parseObject(jSONObject.getJSONObject(TtmlNode.TAG_BODY).toString(), UpdateBean.class);
                            if (!string.equals(updateBean.getVersionName())) {
                                new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { // from class: com.jxw.update.Fragment2.5.1
                                    @Override // java.lang.Runnable
                                    public void run() {
                                        Fragment2.this.ll_new_version.setVisibility(8);
                                        Fragment2.this.ll_no_new_version.setVisibility(8);
                                        Fragment2.this.ll_to_down.setVisibility(0);
                                        Fragment2.this.tv_update_content.setText(updateBean.getDescription());
                                        TextView textView2 = Fragment2.this.tv_newVersion_down;
                                        textView2.setText("新版本：" + updateBean.getVersionName());
                                        TextView textView3 = Fragment2.this.tv_newVersion_size_down;
                                        textView3.setText("文件大小：" + Util.getPrintSize(Long.parseLong(updateBean.getPackageSize())));
                                        Fragment2.this.SYS_DOWN_URL = updateBean.getUrl();
                                        Fragment2.this.versionCode = updateBean.getVersionCode();
                                        Fragment2.this.apkName = Fragment2.this.SYS_DOWN_URL.substring(Fragment2.this.SYS_DOWN_URL.lastIndexOf(InternalZipConstants.ZIP_FILE_SEPARATOR));
                                    }
                                }, 1000L);
                            }
                        } else {
                            new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { // from class: com.jxw.update.Fragment2.5.2
                                @Override // java.lang.Runnable
                                public void run() {
                                    Fragment2.this.ll_new_version.setVisibility(8);
                                    Fragment2.this.ll_no_new_version.setVisibility(0);
                                    TextView textView2 = Fragment2.this.tv_no_version;
                                    textView2.setText("当前版本：" + string);
                                    Fragment2.this.tv_no_version.setTextSize(15.0f);
                                }
                            }, AdaptiveTrackSelection.DEFAULT_MIN_TIME_BETWEEN_BUFFER_REEVALUTATION_MS);
                        }
                    } catch (JSONException e2) {
                        e2.printStackTrace();
                    }
                }
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void install() {
        Intent intent = new Intent();
        intent.setClassName("com.dosmono.upgrade", "com.dosmono.upgrade.Launcher");
        intent.putExtra("upgradeType", 0);
        intent.putExtra("packetPath", this.DOWN_PATH + this.apkName);
        intent.putExtra("package", "");
        intent.putExtra("AppVersion", this.versionCode);
        intent.addFlags(C.ENCODING_PCM_MU_LAW);
        startActivity(intent);
    }
}
